<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Config extends Controller {

    function __construct(){
        parent::Controller();
		if(!models\Current_user::get_user()) 
			show_error(array('Halaman ini tidak bisa diakses oleh anda!',anchor('','Kembali ke halaman utama')),403);

		$this->sidebar_menu_links['index'] = 'Home';
		$this->sidebar_menu_links['ganti_password'] = 'Ganti Password';
		$this->sidebar_menu_links['ganti_email'] = 'Ganti Email';

		if(models\Current_user::previlege('Administrator')){
			$this->sidebar_menu_links['email'] = 'Email Gateway';
			$this->sidebar_menu_links['objek_pengecekan'] = 'Objek Pengecekan';
		}
    }

    function index(){
		$this->headbar->add_header('Configuration > Home');
		$this->sidebar->create_menu($this->sidebar_menu_links);
		$this->load->view('template', array('title' => 'Configuration'));
    }

	function ganti_password(){
		$this->headbar->add_header('Configuration > Ganti Password');
		$this->sidebar->create_menu($this->sidebar_menu_links);
		$this->main_content->add_custom('p','Mengganti password Akun');
		$this->main_content->add_block()
			->add_password_field('password_lama','Password Lama')
			->add_password_field('password_baru','Password Baru')
			->add_password_field('password_confirm','Konfirmasi Password Baru')
			->set_form($this->uri->segment(1).'/ajax_ganti_password','ganti_password');
		$this->main_content->add_script('ganti_password');
		$this->load->view('template', array('title' => 'Configuration'));
	}

	function ajax_ganti_password(){
		if(IS_AJAX){
			$this->load->library('form_validation');
			if($this->form_validation->run('ganti_password') == TRUE){
				$akun = models\Current_user::get_user();
				$akun->set_password($this->input->post('password_baru'));
				$this->doctrine->em->flush();
				$returned_data = array(
					'status' => 'sukses',
				);
			} else {
				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}
			echo json_encode($returned_data);
		} else show_404();
	}

	function ganti_email(){
		$this->headbar->add_header('Configuration > Ganti Email');
		$this->sidebar->create_menu($this->sidebar_menu_links);
		$this->main_content->add_custom('p','Mengganti email Akun');
		$this->main_content->add_block()
			->add_text_field('email','Email Baru')
			->set_form($this->uri->segment(1).'/ajax_ganti_email','ganti_email');
		$this->main_content->add_script('ganti_password');
		$this->load->view('template', array('title' => 'Configuration'));
	}

	function ajax_ganti_email(){
		if(IS_AJAX){
			$this->load->library('form_validation');
			if($this->form_validation->run('ganti_email') == TRUE){

				$new_email = $this->input->post('email');

				$akun = models\Current_user::get_user();
				$akun->set_email($new_email);
				$this->doctrine->em->flush();

				// sementara
				$this->session->set_userdata(array('username' => $new_email));

				$returned_data = array(
					'status' => 'sukses',
				);
			} else {
				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}
			echo json_encode($returned_data);
		} else show_404();
	}

	function email(){
		$this->headbar->add_header('Configuration > Email Gateway');
		$this->sidebar->create_menu($this->sidebar_menu_links);
		$this->main_content->add_custom('p','Akun email yang digunakan untuk mengirim email');
		$this->load->view('template', array('title' => 'Configuration'));
	}

	function objek_pengecekan(){
		$this->headbar->add_header('Configuration > Objek Pengecekan');
		$this->sidebar->create_menu($this->sidebar_menu_links);
		$this->main_content->add_custom('p','Objek pengecekan default untuk tiap jenis device diatur disini');
		$this->load->view('template', array('title' => 'Configuration'));
	}

	function log(){
		$this->headbar->add_header('Configuration > Log');
		$this->sidebar->create_menu($this->sidebar_menu_links);
		$this->main_content->add_custom('p','Daftar user yang login');
		$this->load->view('template', array('title' => 'Configuration'));
	}
}
